package use;

import swimming.DBUtils;
import swimming.consumption;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class consumptionuse {
    public List<consumption> 搜索(String input) throws SQLException{
        List<consumption> list=new ArrayList<>();
        Connection con=DBUtils.getConnection();
        PreparedStatement st= con.prepareStatement("select  * from 消费情况 where 电话=?");
        st.setString(1,input);
        ResultSet re=st.executeQuery();
        list=result(con,st,re);
        return list;
    }
    public List<? extends Object> 会员卡充值(String []input)
    {try{
        List<consumption> list=new ArrayList<>();
        Connection con=DBUtils.getConnection();
        PreparedStatement st= con.prepareStatement("insert into 消费情况(存款金额,结存金额,时间,电话,消费金额) value(?,?,?,?,0)");
        st.setString(1,input[0]);
        st.setString(2,input[1]);
        st.setString(3,input[2]);
        st.setString(4,input[3]);
        st.executeUpdate();
        st= con.prepareStatement("select  * from 消费情况");
        ResultSet re=st.executeQuery();
        list=result(con,st,re);
        return list;}
    catch (SQLException E) {
        return null;
    }
    }
    public List<? extends Object> 折扣卡(String []input)
    {try {
        List<consumption> list = new ArrayList<>();
        Connection con = DBUtils.getConnection();
        PreparedStatement st = con.prepareStatement("insert into 消费情况(存款金额,结存金额,时间,电话,消费金额) value(0,0,?,?,?)");
        st.setString(1, input[0]);
        st.setString(2, input[1]);
        st.setString(3, input[2]);
        st.executeUpdate();
        st = con.prepareStatement("select  * from 消费情况");
        ResultSet re = st.executeQuery();
        list = result(con, st, re);
        return list;
    }
    catch (SQLException E) {
        return null;
    }
    }
    public List<? extends Object> 储值卡(String []input)
    {
        try {
            List<consumption> list = new ArrayList<>();
            Connection con = DBUtils.getConnection();
            PreparedStatement st = con.prepareStatement("insert into 消费情况(存款金额,结存金额,时间,电话,消费金额) value(0,?,?,?,?)");
            st.setString(1, input[0]);
            st.setString(2, input[1]);
            st.setString(3, input[2]);
            st.setString(4, input[3]);
            st.executeUpdate();
            st = con.prepareStatement("select  * from 消费情况");
            ResultSet re = st.executeQuery();
            list = result(con, st, re);
            return list;
        }
        catch (SQLException E) {
            return null;
        }
    }

    public List<consumption> result(Connection con,PreparedStatement st,ResultSet re) throws SQLException
    {
        List<consumption> list=new ArrayList<>();
        try{
            while(re.next())
            {
                String depositamount=re.getString("存款金额");
                String balanceamount=re.getString("结存金额");
                String time=re.getString("时间");
                String cardnum=re.getString("电话");
                String consumeamount=re.getString("消费金额");
                consumption consume=new consumption(depositamount,cardnum,time,balanceamount,consumeamount);
                list.add(consume);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally {
            DBUtils.close(con,st,re);
        }
        return list;
    }
}
